package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;
import aym.util.sdcard.SdcardHelper;
import aym.util.versionup.IVersionSelectedCallBack;
import aym.util.versionup.VersionUpdateUtil;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.Confing;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.view.WihteRoundCornersDialog;

import java.util.List;

/**
 * Created by Jaelyn on 16/2/25.
 * 设置
 */
public class SetActivity extends BaseUIActivity {
    /**
     * content layout
     */
    @ViewInject(id = R.id.content_info_layout)
    private LinearLayout content_info_layout;
    /**
     * 账号管理
     */
    @ViewInject(id = R.id.accountManagerLayout, click = "accountManagerLayoutClick")
    private LinearLayout accountManagerLayout;
    /**
     * 安全管理
     */
    @ViewInject(id = R.id.securityLayout, click = "securityLayoutClick")
    private LinearLayout securityLayout;
    /**
     * 关于我们
     */
    @ViewInject(id = R.id.tr_my_about_us, click = "tr_my_about_usClick")
    private LinearLayout tr_my_about_us;
    /**
     * 帮助中心
     */
    @ViewInject(id = R.id.tr_my_help_center, click = "tr_my_help_centerClick")
    private LinearLayout tr_my_help_center;
    /**
     * 意见反馈
     */
    @ViewInject(id = R.id.tr_my_feedback, click = "tr_my_feedbackClick")
    private LinearLayout tr_my_feedback;
    /**
     * 版本检测
     */
    @ViewInject(id = R.id.tr_my_version_checking, click = "tr_my_version_checkingClick")
    private LinearLayout tr_my_version_checking;

    @ViewInject(id = R.id.my_the_current_version)
    private TextView my_the_current_version;
    /**
     * 联系我们
     */
    @ViewInject(id = R.id.tr_my_contact_us, click = "tr_my_contact_usClick")
    private LinearLayout tr_my_contact_us;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_layout);
        setCenter_title(R.string.systemSet);
        my_the_current_version.setText("V " + myApplication.getVersionName());
        util = VersionUpdateUtil.init(mContext);
        sdHelper = new SdcardHelper();
    }

    public void accountManagerLayoutClick(View view) {

    }

    public void securityLayoutClick(View view) {
        jumpTo(UserChangeMainActivity.class);
    }

    public void tr_my_about_usClick(View view) {
        Intent intent = new Intent(mContext, UrlWebviewActivity.class);
        intent.putExtra(ExtraKeys.Key_Msg1, 7);
        jumpTo(intent, false);
    }

    public void tr_my_help_centerClick(View view) {
        Intent intent = new Intent(mContext, UrlWebviewActivity.class);
        intent.putExtra(ExtraKeys.Key_Msg1, 1);
        jumpTo(intent, false);
    }

    public void tr_my_feedbackClick(View view) {
        jumpTo(UserFeedBackActivity.class);
    }

    public void tr_my_version_checkingClick(View view) {
        upversion();
    }

    public void tr_my_contact_usClick(View view) {
        jumpTo(ContactUsActivity.class);
    }


    // 版本升级的工具
    private VersionUpdateUtil util;
    // 内存卡管理的帮助类
    private SdcardHelper sdHelper;

    /**
     * 检测版本
     */
    private void upversion() {
        util = VersionUpdateUtil.init(mContext);
        if (!sdHelper.ExistSDCard()) {
            toast.showToast(R.string.nosdcard);
        } else {
            util.doSelectVersion(GetDataConfing.ip,
                    GetDataConfing.Action_selectAppVersionInfo, null, false,
                    versionSelectedCallBack);
        }

    }

    String apkDownLoadPath;
    WihteRoundCornersDialog builder;

    /**
     * 提示用户有新版本
     */
    private void showNewVersion(JsonMap<String, Object> info,
                                String versionName, int versionCode) {
        apkDownLoadPath = info.getStringNoNull("VersionPath");
        builder = new WihteRoundCornersDialog(mContext,
                R.style.ExitDialogStyle, 2, callBackdialog1);
        builder.setTitletext(R.string.app_version_select);
        builder.setMessagetext(info.getStringNoNull("Description"));
        builder.setButtonText(R.string.app_version_iknow, R.string.app_version_update);
        builder.show();
    }

    private WihteRoundCornersDialog.DialogCallBack callBackdialog1 = new WihteRoundCornersDialog.DialogCallBack() {

        @Override
        public void bttonclick(int index) {
            builder.dismiss();
            switch (index) {
                case 1:

                    break;
                case 2:
                    String appName = getString(R.string.app_name);
                    util.doUpdateVersion(apkDownLoadPath,
                            Confing.productPath + appName + ".apk",
                            appName, R.drawable.app_icon, true);

                    break;
                default:
                    break;
            }
        }

        @Override
        public void bttonclick(int index, int what) {

        }

    };

    IVersionSelectedCallBack versionSelectedCallBack = new IVersionSelectedCallBack() {
        @Override
        public void netError(String arg0) {
            toast.showToast(R.string.neterror);
        }

        @Override
        public boolean isUpdate(String serviceInfo, String versionName,
                                int versionCode) {
            try {
                serviceInfo = serviceInfo.substring(serviceInfo.indexOf("{"),
                        serviceInfo.lastIndexOf("}") + 1);
            } catch (Exception e) {
                e.printStackTrace();

                return false;
            }
            if (ShowGetDataError.isCodeIsNot1(mContext, serviceInfo)) {
                List<JsonMap<String, Object>> infoData = JsonParseHelper
                        .getJsonMap_List_JsonMap(serviceInfo, JsonKeys.Key_Info);
                if (infoData.size() == 0) {

                } else {
                    JsonMap<String, Object> info = infoData.get(0);
                    if (info.getStringNoNull("VersionNo").equals(versionName)
                            || "".equals(info.getStringNoNull("VersionNo"))
                            || "".equals(info.getStringNoNull("VersionPath"))) {
                        toast.showToast(R.string.app_version_newest);
                    } else {
                        showNewVersion(info, versionName, versionCode);
                    }
                }

            } else {

            }
            return false;
        }
    };


}